package com.happyass.controller;

import com.happyass.pojo.Staff;
import com.happyass.pojo.User;
import com.happyass.service.StaffInfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    StaffInfoService staffInfoService;

    //个人信息
    @RequestMapping("/myInfo")
    public String myInfo(Model model) {
        Subject subject = SecurityUtils.getSubject();
        Staff staffTemp = (Staff) subject.getPrincipal();
        Integer staffId = staffTemp.getStaffId();
        Staff Info = staffInfoService.findByStaffId(staffId);
        model.addAttribute("Info", Info);
        return "Staff/myInfo";
    }



    //所有员工信息
    @RequestMapping("/staffInfos")
    public String StaffInfos(Model model) {
        List<Staff> staffList = staffInfoService.getStaffInfos();
        model.addAttribute("staffList", staffList);
        return "/Table/staffInfosTable";
    }

//    //批量删除
//    @ResponseBody
//    @RequestMapping("/delAll")
//    public HashMap<String,Object> delAll(@RequestParam("StaffId")Integer StaffId){
//        return staffInfoService.del(StaffId);
//    }


    //删除信息
    @ResponseBody
    @RequestMapping("/del")
    public boolean delStaff(@RequestParam("StaffId") Integer StaffId) {
        return staffInfoService.del(StaffId);
    }

    //访问增加页面
    @RequestMapping("/add")
    public String addStaff() {
        return "/Staff/staffAdd";
    }

    @ResponseBody
    @RequestMapping("/submitAdd")
    public boolean submitAdd(Staff staff) {
        return staffInfoService.add(staff);
    }

    //访问修改页面
    @RequestMapping("/edit")
    public String EditInfo(@RequestParam(value = "staffId", required = false) Integer staffId, Model model) {
        Staff info = staffInfoService.findByStaffId(staffId);
        model.addAttribute("info", info);
        return "/Staff/staffEdit";
    }

    //修改信息
    @ResponseBody
    @RequestMapping("/submitEdit")
    public HashMap<String, Object> editSubmit(Staff staff) {
        Boolean info = staffInfoService.update(staff);
        HashMap<String, Object> map = new HashMap<>();
        if (info){
            map.put("info","修改成功");
            return map;
        }
        map.put("info","修改失败");
        return map;
    }

    //修改密码
    @RequestMapping("/editPassword")
    public String edit(Model model){
        Subject subject = SecurityUtils.getSubject();
        Staff staffTemp = (Staff) subject.getPrincipal();
        Integer staffId = staffTemp.getStaffId();
        Staff staffInfo = staffInfoService.selectById(staffId);
        model.addAttribute("staff",staffInfo);
        return "common/passwordEdit";
    }
    //保存
    @RequestMapping("/save/{path}")
    @ResponseBody
    public boolean save(String currentPassword,String newPassword, @PathVariable("path") String path){
        //修改
        return staffInfoService.edit(currentPassword,newPassword);
    }
}